﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls; 

namespace CoolCode.Web.UI
{
    internal  class BaseControlManager 
    {
        private Page page;

        public Page Page { get { return page; } set { page = value; } }

        private List<Control> _controls;
        public List<Control> AllControls
        {
            get
            {
                if (_controls == null)
                    _controls = page.Form.GetControls();
                return _controls;
            }
        }

        public BaseControlManager()
        {

        }

        public BaseControlManager(Control container) 
        { 
                 page = container is Page ?  (Page)container : container.Page ; 
        }

        public virtual void  LoadJs( string jsFileName)
        { 
            if (!page.ClientScript.IsClientScriptIncludeRegistered(page.GetType(), jsFileName))
            {
                page.ClientScript.RegisterClientScriptInclude(page.GetType(), jsFileName, jsFileName); 
            } 
        }

        public virtual void LoadAllJs(  string jsDir)
        {
            string mapPath = Utility.GetMapPath(jsDir);
            string[] jsFiles = Directory.GetFiles(mapPath); 
            string virDir = jsDir.EndsWith("/") ? jsDir : jsDir + "/";
            if (jsFiles != null && jsFiles.Length > 0)
            {
                jsFiles = jsFiles.Where(c => c.EndsWith(".js")).ToArray();//filter              
                foreach (string f in jsFiles)
                {
                   string virfile = virDir + Path.GetFileName(f);
                   LoadJs(virfile);
                }
            } 
        }

        public virtual void LoadCss( string cssFile)
        {
            HtmlGenericControl objLink = new HtmlGenericControl("LINK"); 
            objLink.Attributes["rel"] = "stylesheet";
            objLink.Attributes["type"] = "text/css";
            objLink.Attributes["href"] = cssFile;
            page.Header.Controls.Add(objLink);
        }

        public virtual void ChangeTheme( )
        {
            string theme = RequestUtility.GetQueryString("theme","Default");
            Page.Theme = theme;
        }


    }
}
